package univ.lille1.fr.rest;

import java.io.IOException;
import java.net.SocketException;

import javax.ws.rs.Consumes;
import javax.ws.rs.FormParam;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import univ.lille1.fr.rest.bean.UserBean;

@Path("/xml/log")
public class LogService {

	private PasserelleREST rest;
	
	@POST
	@Path("/get")
	@Produces(MediaType.APPLICATION_XML)
	public UserBean getUserXML(@FormParam("user") String user,
			@FormParam("password") String password,
			@FormParam("host") String host, @FormParam("port") String port) {
		
		rest = new PasserelleREST(user, password);
		
		try {
			if(port.equals("") || port == null){
				rest.connect(host);
			} else {
				rest.connect(host, Integer.parseInt(port));
			}
		} catch (SocketException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		UserBean bean = this.rest.getBean();
		return bean;
	}

	@POST
	@Path("/post")
	@Consumes(MediaType.APPLICATION_XML)
	public Response createUserXML(UserBean bean) {
		String result = "User : " + bean.toString();
		return Response.status(201).entity(result).build();
	}
}
